home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch17 / Transmit.frm (.txt) < prev    next >
Visual Basic Form  |  1999-06-04  |  6KB  |  194 lines

  1. VERSION 5.00
  2. Begin VB.Form frmTransmit 
  3.    Caption         =   "Transmit"
  4.    ClientHeight    =   3630
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   5235
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   3630
  10.    ScaleWidth      =   5235
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.TextBox txtN2 
  13.       Height          =   285
  14.       Left            =   4380
  15.       TabIndex        =   4
  16.       Text            =   "1.5"
  17.       Top             =   480
  18.       Width           =   735
  19.    End
  20.    Begin VB.TextBox txtN1 
  21.       Height          =   285
  22.       Left            =   4380
  23.       TabIndex        =   2
  24.       Text            =   "1.0"
  25.       Top             =   120
  26.       Width           =   735
  27.    End
  28.    Begin VB.PictureBox picCanvas 
  29.       AutoRedraw      =   -1  'True
  30.       Height          =   3375
  31.       Left            =   120
  32.       ScaleHeight     =   3315
  33.       ScaleWidth      =   3315
  34.       TabIndex        =   0
  35.       Top             =   120
  36.       Width           =   3375
  37.    End
  38.    Begin VB.Label Label1 
  39.       Caption         =   "n2/n1"
  40.       Height          =   255
  41.       Index           =   5
  42.       Left            =   3840
  43.       TabIndex        =   12
  44.       Top             =   2400
  45.       Width           =   495
  46.    End
  47.    Begin VB.Label Label1 
  48.       Caption         =   "Sin1/Sin2"
  49.       Height          =   255
  50.       Index           =   4
  51.       Left            =   3600
  52.       TabIndex        =   11
  53.       Top             =   2040
  54.       Width           =   735
  55.    End
  56.    Begin VB.Label lblSin1Sin2 
  57.       BorderStyle     =   1  'Fixed Single
  58.       Height          =   255
  59.       Left            =   4380
  60.       TabIndex        =   10
  61.       Top             =   2040
  62.       Width           =   735
  63.    End
  64.    Begin VB.Label lblN2N1 
  65.       BorderStyle     =   1  'Fixed Single
  66.       Height          =   255
  67.       Left            =   4380
  68.       TabIndex        =   9
  69.       Top             =   2400
  70.       Width           =   735
  71.    End
  72.    Begin VB.Label lblSin2 
  73.       BorderStyle     =   1  'Fixed Single
  74.       Height          =   255
  75.       Left            =   4380
  76.       TabIndex        =   8
  77.       Top             =   1440
  78.       Width           =   735
  79.    End
  80.    Begin VB.Label lblSin1 
  81.       BorderStyle     =   1  'Fixed Single
  82.       Height          =   255
  83.       Left            =   4380
  84.       TabIndex        =   7
  85.       Top             =   1080
  86.       Width           =   735
  87.    End
  88.    Begin VB.Label Label1 
  89.       Caption         =   "Sin1"
  90.       Height          =   255
  91.       Index           =   3
  92.       Left            =   3960
  93.       TabIndex        =   6
  94.       Top             =   1080
  95.       Width           =   375
  96.    End
  97.    Begin VB.Label Label1 
  98.       Caption         =   "Sin2"
  99.       Height          =   255
  100.       Index           =   2
  101.       Left            =   3960
  102.       TabIndex        =   5
  103.       Top             =   1440
  104.       Width           =   375
  105.    End
  106.    Begin VB.Label Label1 
  107.       Caption         =   "n2"
  108.       Height          =   255
  109.       Index           =   1
  110.       Left            =   4080
  111.       TabIndex        =   3
  112.       Top             =   480
  113.       Width           =   255
  114.    End
  115.    Begin VB.Label Label1 
  116.       Caption         =   "n1"
  117.       Height          =   255
  118.       Index           =   0
  119.       Left            =   4080
  120.       TabIndex        =   1
  121.       Top             =   120
  122.       Width           =   255
  123.    End
  124. Attribute VB_Name = "frmTransmit"
  125. Attribute VB_GlobalNameSpace = False
  126. Attribute VB_Creatable = False
  127. Attribute VB_PredeclaredId = True
  128. Attribute VB_Exposed = False
  129. Option Explicit
  130. Private Const LINE_SCALE = 100
  131. Private Ymid As Single
  132. Private Xmid As Single
  133. Private Sub Form_Load()
  134.     picCanvas.ScaleMode = vbPixels
  135.     Ymid = picCanvas.ScaleHeight / 2
  136.     Xmid = picCanvas.ScaleWidth / 2
  137.     picCanvas.Line (0, Ymid)-Step(picCanvas.ScaleWidth, 0)
  138.     picCanvas.Line (Xmid, Ymid)-Step(0, -LINE_SCALE)
  139.     picCanvas.Picture = picCanvas.Image
  140. End Sub
  141. Private Sub picCanvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  142. Const PI = 3.14159265
  143. Dim nx As Single
  144. Dim ny As Single
  145. Dim n1 As Single
  146. Dim n2 As Single
  147. Dim lx As Single
  148. Dim ly As Single
  149. Dim ltx As Single
  150. Dim lty As Single
  151. Dim dist As Single
  152. Dim LdotN As Single
  153. Dim cos1 As Single
  154. Dim cos2 As Single
  155. Dim n1_over_n2 As Single
  156. Dim normal_factor As Single
  157. Dim theta1 As Single
  158. Dim theta2 As Single
  159.     picCanvas.Cls
  160.     n1 = CSng(txtN1.Text)
  161.     n2 = CSng(txtN2.Text)
  162.     ' Get the normal vector.
  163.     nx = 0
  164.     ny = -1
  165.     ' Get the light vector.
  166.     lx = X - Xmid
  167.     ly = Y - Ymid
  168.     dist = Sqr(lx * lx + ly * ly)
  169.     lx = lx / dist
  170.     ly = ly / dist
  171.     picCanvas.Line (Xmid, Ymid)-Step(LINE_SCALE * lx, LINE_SCALE * ly), vbBlue
  172.     ' Calculate the transmission vector LT.
  173.     LdotN = lx * nx + ly * ny
  174.     cos1 = Abs(LdotN)
  175.     n1_over_n2 = n1 / n2
  176.     cos2 = 1 - (1 - cos1 * cos1) * n1_over_n2 * n1_over_n2
  177.     If cos2 < 0 Then
  178.         ' Reflection.
  179.     Else
  180.         cos2 = Sqr(cos2)
  181.         ' Note that the incident vector I = -L.
  182.         normal_factor = cos2 - n1_over_n2 * cos1
  183.         ltx = -n1_over_n2 * lx - normal_factor * nx
  184.         lty = -n1_over_n2 * ly - normal_factor * ny
  185.     End If
  186.     picCanvas.Line (Xmid, Ymid)-Step(LINE_SCALE * ltx, LINE_SCALE * lty), vbYellow
  187.     theta1 = PI / 2 - Atn(ly / lx)
  188.     lblSin1.Caption = Format$(Sin(theta1))
  189.     theta2 = PI / 2 - Atn(lty / ltx)
  190.     lblSin2.Caption = Format$(Sin(theta2))
  191.     lblSin1Sin2.Caption = Format$(Sin(theta1) / Sin(theta2))
  192.     lblN2N1.Caption = Format$(n2 / n1)
  193. End Sub
  194.